package com.ufgov.agcfs.manage.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import uap.iweb.exception.WebRuntimeException;
import uap.iweb.icontext.IWebViewContext;

import com.ufgov.agcfs.manage.entity.Company;
import com.ufgov.agcfs.manage.entity.CompanyNode;
import com.ufgov.agcfs.manage.service.CompanyService;

@Controller("manage.company.companyController")
@Scope("prototype")
@RequestMapping(value = "/manage/company")
public class CompanyController {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private CompanyService companyService;
	
	@RequestMapping(method = RequestMethod.GET,value="getCompanyList")
	public  @ResponseBody Map<String,Object> getCompanyList(HttpServletRequest request){
		try {
			Map<String,String> condMap = new HashMap<String, String>();
			Map<String,Object> hashRet = new HashMap<String, Object>();
			String searchCond = request.getParameter("extra_search");
			if ((searchCond!=null)&&(!searchCond.equals(""))){
				ObjectMapper mapper = new ObjectMapper();  
				condMap = mapper.readValue(searchCond,JSONObject.class);//转成map 
				List<Company> comList = companyService.getCompanyList(condMap);
				hashRet = new HashMap<String, Object>();
				hashRet.put("data",comList);
			}
			return hashRet;
		} catch (Exception e) {
			logger.error(e.getMessage());
			IWebViewContext.getResponse().write("false");
			throw new WebRuntimeException("查询数据失败!");
		}		
	}

	@RequestMapping(method = RequestMethod.GET,value="getCompany")
	public @ResponseBody List<CompanyNode> geCompanyList(HttpServletRequest request){
		try {
//			String id = request.getParameter("id");
			String id = "";
			if ((id!=null)&&(!id.equals(""))){
				return companyService.getCompanyByParentCoId(id);
			} else
				return companyService.getCompanyNodes();
		} catch (Exception e) {
			logger.error(e.getMessage());
			IWebViewContext.getResponse().write("false");
			throw new WebRuntimeException("查询数据失败!");
		}		
	}
	
	@RequestMapping(method = RequestMethod.POST,value="saveCompany")
	public @ResponseBody Map<String, String> saveCompany(@RequestBody Company company){
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("result", "success");
		resultMap.put("reason", "保存成功！");
		try {
			companyService.saveCompany(company,resultMap);
		} catch (Exception e) {
			logger.error(e.getMessage());
			resultMap.put("result", "fail");
			resultMap.put("reason", "保存失败，请稍后重试！");
		}
		return resultMap;		
	}
}
